<?php
/**
 * 分类增加保存功能
 */

session_start();
if (empty($_SESSION['admin_email'])) {
    echo '尚未登录，请先登录 <a href="index.php?c=login">登录</a>';
    exit();
}

// 获取到分类数据
$categoryName = $_POST['category_name'];
$categoryDesc = $_POST['category_desc'];

// 2~45
if (mb_strlen($categoryName) < 2 || mb_strlen($categoryName) > 45) {
    echo '分类名称2~45个字符。';
    echo '<a href="javascript:void(0)" onclick="history.back();">返回上一页</a>';
    exit();
}
// 10~255
if (mb_strlen($categoryDesc) < 10 || mb_strlen($categoryDesc) > 255) {
    echo '分类描述10~255个字符。';
    echo '<a href="javascript:void(0)" onclick="history.back();">返回上一页</a>';
    exit();
}

include_once APP_PATH . "./model/category_add_save.php";

if ($result) {
    $log = [
        'action' => 'category_add',
        'content' => '增加分类成功',
        'time' => date("Y-m-d H:i:s", time()),
        'ip' => $_SERVER['REMOTE_ADDR'],
        'category_name' => $categoryName,
        'category_desc' => $categoryDesc,
    ];
    $file = fopen("./logs/" . date("Y-m-d") . ".txt", 'a+');
    if ($file) {
        fwrite($file, json_encode($log, JSON_UNESCAPED_UNICODE) . PHP_EOL);
        fclose($file);
    } else {
        echo "打开文件错误";
        exit();
    }

    echo "插入成功。<a href='index.php?c=category_list'>返回列表页</a>";
    exit();
} else {
    echo "插入失败，错误信息：" . $db->errorInfo()[2] . "，请联系管理员：303410541@qq.com";
}
